var oprateUrl=DOMAIN +'/v1/supplier'+isLogin;
$(document).ready(function() {
	loadTable();
	
	getSelList($("#currency"),"currency");
	getSelList2($("#supplierType"),"supplier_type");
	getSelList($("#supplierTypeAdd"),"supplier_type");
	getSelList($("#defaultOrderType"),"order_type");
	getSelList($("#defaultReceivingType"),"receiving_type");
	//查询
	$("#searchBtn").click(function() {
		//新查询参数
		function queryParamNew(params) {
			var params = {};
			params.code = $("#code").val();
			params.name = $("#name").val();
			params.supplierType = $("#supplierType option:checked").val();
			return params;
		};
		$('#table').bootstrapTable(
			'refreshOptions', {
				url: oprateUrl,
				//查询参数
				queryParams: queryParamNew
			}
		);

	});
});

function loadTable() {
	$('#table').bootstrapTable({
		url: oprateUrl,
		method: 'get',
		cache: false,
		striped: true,
		pagination: true,
		pageSize: 50,
		pageNumber: 1,
		pageList: [50, 100],
		search: false,
		showColumns: false,
		clickToSelect: true,
		queryParams: {},
		smartDisplay: false,
		columns: [{
			field: "id",
			title: "id",
			align: "center",
			valign: "middle",
			visible: false
		},{
			field: 'code',
			title: "系统编码",
			align: "center",
			valign: "middle"
		},{
			field: 'name',
			title: "商户名称",
			align: "center",
			valign: "middle"
		},{
			field: 'fax',
			title: "商户代码",
			align: "center",
			valign: "middle"
		},{
			field: 'currencyName',
			title: "交易货币",
			align: "center",
			valign: "middle"
		},{
			field: "currency",
			title: "货币代码",
			align: "center",
			valign: "middle",
			visible: false
		}/*, {
			field: "integral",
			title: "积分",
			align: "center",
			valign: "middle"
		}*/, {
			field: "defaultOrderTypeName",
			title: "默认订单类型",
			align: "center",
			valign: "middle"
		},{
			field: "defaultOrderType",
			title: "默认订单类型代码",
			align: "center",
			valign: "middle",
			visible: false
		}, {
			field: "defaultReceivingTypeName",
			title: "默认收货方式",
			align: "center",
			valign: "middle"
		},{
			field: "defaultReceivingType",
			title: "默认收货方式代码",
			align: "center",
			valign: "middle",
			visible: false
		}, {
			field: "supplierTypeName",
			title: "商户类型",
			align: "center",
			valign: "middle"
		},{
			field: "supplierType",
			title: "商户类型代码",
			align: "center",
			valign: "middle",
			visible: false
		}/*, {
			field: "contractPhone",
			title: "联系电话",
			align: "center",
			valign: "middle"
		}*/,{
			field: 'telephone',
			title: "手机号",
			align: "center",
			valign: "middle"
		}/*,{
			field: 'fax',
			title: "传真",
			align: "center",
			valign: "middle"
		}, {
			field: "email",
			title: "邮箱",
			align: "center",
			valign: "middle"
		},{
			field: 'qq',
			title: "QQ",
			align: "center",
			valign: "middle"
		}, {
			field: "address",
			title: "地址",
			align: "center",
			valign: "middle"
		}, {
			field: "remarks",
			title: "备注",
			align: "center",
			valign: "middle"
		}*/, {
			field: "",
			title: "操作",
			align: "center",
			valign: "middle",
			events: funOprate,
			formatter: function(value,row,index) { 
				return '<button data-toggle="modal" data-edit="div-modal" data-target="#div-modal" class="btn btn-primary btn-sm edit" onclick="showBox();">&emsp;编辑</button> <button data-del="'+oprateUrl+'" class="btn btn-primary btn-sm del">&emsp;删除</button>';
	        } 
		}],
		//>>>>>>>>>>>>>>导出excel表格设置
		showExport: true,
		showExport: true,
		//是否显示导出按钮
		buttonsAlign: "left",
		//按钮位置
		exportDataType: 'all',
		//导出的方式 all全部 selected已选择的  basic', 'all', 'selected'.
		Icons: 'glyphicon glyphicon-export',
		exportTypes: ['excel', 'doc', 'xlsx', 'txt',],
		//导出文件类型
		exportOptions: {
			ignoreColumn: [0, 0],  //忽略某一列的索引

			fileName: '导出数据表',
			//文件名称设置
			worksheetName: 'sheet1',
			//表格工作区名称
			tableName: '导出数据表',
			// excelstyles: ['background-color', 'color', 'font-size', 'font-weight'], 设置格式

		},
		//导出excel表格设置<<<<<<<<<<<<<<<<
		formatNoMatches: function () {
			return '没有找到匹配的记录';
		},
		formatNoMatches: function () {
			return '没有找到匹配的记录';
		}
	});
};

//保存
function funSave(){
    var id = $("#id").val();
	var type ="";
	var data = $("#form-modal").serialize();
    if(isNotEmpty(id)){//修改
    	type="PATCH";
    }else{//新增
    	type="POST";
    }
    $.ajax({
        type : type,
        url : oprateUrl,
        data:data,
        async: false,  
        success : function(result) {
        	if (result.status == 200) {
        		$('.close').click();
        		document.getElementById("form-modal").reset();
    			$("#id").val("");
    			alert("保存成功!");
	        	$("#table").bootstrapTable("refresh", {
			     silent: true //静态刷新
			    });
			}
        	if (result.status == 402) {
        		alert(result.msg);
			}
        },
        error : function() {
        	alert("修改失败！");
        }
    });
};
//保存
$("#btn-save").click(function() {
	if(isEmpty($("#pname").val())){
		alert("商户名称不能为空!");
		return;
	}
	if(isEmpty($("#supplierTypeAdd").val())){
		alert("商户类型不能为空!");
		return;
	}
	funSave();
});

//关闭
$("#btn-close").click(function() {
    document.getElementById("form-modal").reset();
    $("#id").val("");
});

$("#Btn-insert").click(function() {
	 $("#id").val("");
     $("#form-modal")[0].reset();
     $("#codeBox").css("display","none");
     //$("#faxBox").css("display","block");
     $("#modalTitle").text("新增");
});

function showBox(){
     $("#codeBox").css("display","block");
     //$("#faxBox").css("display","none");
     $("#modalTitle").text("修改");
}